package com.musifind.activities;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.fizy.client.controllers.FizyControllers;
import com.fizy.client.objects.Track2;
import com.musifind.adapters.SearchAdapterTracks;
import com.musifind.common.GeneralHelper;
import com.musifind.models.SearchItems;

public class TracksFragment extends BaseFragment {
	private ListView lstSearchTracks;
	static ImageView imgLogo;
	private ProgressDialog progressDialog = null;
	private int lvGlobalPosition = 0;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
		// StrictMode.setThreadPolicy(policy);
		View rootView = inflater.inflate(R.layout.fragment_tracks, container, false);
		imgLogo = (ImageView) rootView.findViewById(R.id.imgBlankPage);

		// imgLogo = (ImageView) rootView.findViewById(R.id.imgBlankPage);
		// if (searchArrayTracks == null || searchArrayTracks.size() == 0) {
		// if (baseFragment instanceof ArtistsFragment && baseFragment.searchArrayArtists != null
		// && baseFragment.searchArrayArtists.size() > 0)
		// baseFragment.imgLogo.setVisibility(View.INVISIBLE);
		// if (baseFragment instanceof AlbumsFragment && baseFragment.searchArrayAlbums != null
		// && baseFragment.searchArrayAlbums.size() > 0)
		// baseFragment.imgLogo.setVisibility(View.INVISIBLE);

		searchAdapterTracks = new SearchAdapterTracks(getActivity(), R.layout.search_list_items_row_tracks,
				searchArrayTracks);
		lstSearchTracks = (ListView) rootView.findViewById(R.id.lstSearchTrackItems);
		lstSearchTracks.setAdapter(searchAdapterTracks);

		lstSearchTracks.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				lvGlobalPosition = position;
				new OpenTrackAsyncTask().execute();
			}
		});
		// } else {
		// imgLogo.setVisibility(View.INVISIBLE);
		// }
		return rootView;
	}

	public void openMusicPlayer() {
		SearchItems item = (SearchItems) searchAdapterTracks.getItem(lvGlobalPosition);
		FizyControllers controllers = new FizyControllers();
		Track2 track2 = controllers.getTrack(String.valueOf(item.getTrackId()));
		if (track2 != null) {
			Intent i = new Intent(getActivity(), MusicPlayerActivity.class);
			i.putExtra("track_id", String.valueOf(track2.getId()));
			i.putExtra("track_url", track2.getSecure_url());
			i.putExtra("track_name", track2.getName());
			i.putExtra("artist_name", track2.getArtist().getName());
			i.putExtra("album_name", track2.getAlbum().getName());
			i.putExtra("album_year", track2.getAlbum().getRelease_date());
			i.putExtra("cover_image", track2.getAlbum().getCover_large());
			startActivity(i);
		} else {
			GeneralHelper.showRedToast(getActivity(), "URL bulunamadı.");
		}
	}

	public class OpenTrackAsyncTask extends AsyncTask<String, Void, String> {
		@Override
		protected void onPreExecute() {
			progressDialog = new ProgressDialog(getActivity());
			progressDialog.setTitle("Çalmaya hazırlanıyor");
			progressDialog.setMessage("Lütfen bekleyiniz...");
			progressDialog.setCancelable(false);
			progressDialog.show();
		}

		@Override
		protected String doInBackground(String... params) {
			openMusicPlayer();
			return null;
		}

		@Override
		protected void onPostExecute(String s) {
			progressDialog.dismiss();
		}
	}
}
